#include "setbutton.h"
#include "configfile.h"
#include <QCheckBox>
#include <QFileDialog>
#include <QFontComboBox>
#include <QFontDialog>
#include <QLabel>
void MainWindow::handleSoftwareSet() {
  // 设置弹窗
  QDialog dialog(this);
  // 设置标签页的标题字体
  QFont font;
  font.setBold(true);
  // 设置要重置的默认字体
  QFont reset_font("微软雅黑", 10);
  // 创建标签
  QTabWidget *tabWidget = new QTabWidget(&dialog); // 设置tab标签页的父窗口
  // 创建第一个tab标签
  QWidget *tab1 = new QWidget();
  tabWidget->addTab(tab1, QIcon(), "基础设置");
  // 创建布局
  QVBoxLayout *layout1 = new QVBoxLayout(tab1);
  // 标题
  QLabel *lrcset = new QLabel();
  lrcset->setText("歌词部分");
  lrcset->setFont(font);
  // 分割线
  QFrame *horizontalLine = new QFrame();
  horizontalLine->setFrameShape(QFrame::HLine); // 水平线
  horizontalLine->setLineWidth(1);              // 线宽
  // 按钮
  QCheckBox *lyrbtn = new QCheckBox();
  lyrbtn->setText("歌词显示");
  // 设置复选按钮的初始状态
  if (ui->lyric_display->isHidden()) {
    lyrbtn->setChecked(false);
  } else {
    lyrbtn->setChecked(true);
  }
  // 歌词出现和隐藏功能
  connect(lyrbtn, &QCheckBox::toggled, [this](bool checked) {
    if (checked) {
      ui->lyric_display->show();
      ui->song_title->show();
      SaveConfigFile("LrcSet", "lrcShow", "true");
    } else {
        ui->song_title->show();
        ui->lyric_display->hide();
      SaveConfigFile("LrcSet", "lrcShow", "false");
    }
  });
  //标题(字体设置)
  QLabel *fontset = new QLabel();
  fontset->setText("字体部分");
  fontset->setFont(font);
  //选择字体
  QHBoxLayout *font_choice_layout = new QHBoxLayout();//横向布局
  QLabel *fontstyle = new QLabel();
  fontstyle->setText(qApp->font().family() +" "+
                     QString::number(qApp->font().pointSize()));//字体样式和大小
  QPushButton *fontchoice=new QPushButton("选择字体");
  fontchoice->setIcon(QIcon(":/icons/字体.png"));
  connect(fontchoice,&QPushButton::clicked,[=]
    {
      QFontDialog font_dialog;//设置字体对话框的大小
      font_dialog.setOption(QFontDialog::DontUseNativeDialog);
      font_dialog.resize(500,500);
      font_dialog.setCurrentFont(QFont(qApp->font().families(), qApp->font().pointSize()));

      if(font_dialog.exec()==QFontDialog::Accepted)//如果没有取消选择
      {
      QFont font = font_dialog.selectedFont();
      qDebug() << "选中的字体:" << font.family() << "大小:" << font.pointSize();
      qDebug() << font;
      FontSet(font, ui);
      custom_song_list->setFont(font);
      fontstyle->setText(qApp->font().family() + " "+
                         QString::number(qApp->font().pointSize()));//字体样式和大小
      SaveConfigFile("FontSet", "fontstyle","font");
      }
    });
  font_choice_layout->addWidget(fontstyle);
  font_choice_layout->addStretch();
  font_choice_layout->addWidget(fontchoice);
  //字体大小设置按钮布局
  //QHBoxLayout *font_size_layout = new QHBoxLayout();
  //QList<QRadioButton *> bottons = FontSizeChange(font_size_layout, ui);
  //重置字体样式以及大小的按钮
  QPushButton *resetfont = new QPushButton(tab1);
  resetfont->setText("字体重置");
  connect(resetfont, &QPushButton::clicked,
          [=]() {
            FontSet(reset_font, ui);
            qDebug() << reset_font;
            //GetFontSize(bottons);//获得字体尺寸,然后更改按钮选项
            fontstyle->setText(qApp->font().family() +" "+
                               QString::number(qApp->font().pointSize()));//字体样式和大小
            SaveConfigFile("FontSet", "fontstyle","font");
          });
  // -------------------------
  QFrame *horizontalLine1 = new QFrame();
  horizontalLine1->setFrameShape(QFrame::HLine); // 水平线
  horizontalLine1->setLineWidth(1);              // 线宽
  //标题
  //背景
  QLabel *bgset = new QLabel(tab1);
  bgset->setText("背景部分");
  bgset->setFont(font);
  //-------------------------
  QFrame *horizontalLine2 = new QFrame();
  horizontalLine2->setFrameShape(QFrame::HLine); // 水平线
  horizontalLine2->setLineWidth(1);              // 线宽
  // 背景选择按钮
  QHBoxLayout *bakcground_set_layout=new QHBoxLayout();
  //QLabel *bgpathprefix =new QLabel("背景路径:");
  //bgpathprefix->fontInfo().pointSize();
  QLabel *bgpath =new QLabel(GetBackGround());
  bgpath->setMinimumWidth(40);
  bgpath->setWordWrap(true);
  bgpath->setAlignment(Qt::AlignLeft);
  QPushButton *bgbtn = new QPushButton();
  bgbtn->setFixedSize(40,40);
  bgbtn->setIcon(QIcon(":/icons/文件.png"));
  bgbtn->setIconSize(QSize(bgbtn->width() - 15, bgbtn->height() - 15));
  connect(bgbtn, &QPushButton::clicked, [=]() {
    // 设置背景
    QString imageAbsPath = BackgroungChoice(tab1);

    if (!imageAbsPath.isNull())
    {
      bgpath->setToolTip(imageAbsPath);
      bgpath->setText(imageAbsPath);
      setBackground(imageAbsPath);

      SaveConfigFile("BackGroundSet", "imagepath", imageAbsPath);
    }
  });
  QPushButton *bgreset = new QPushButton("默认背景");
  connect(bgreset, &QPushButton::clicked, [=]() {
      // 设置背景
    QString imageAbsPath =":/image/bk1.jpg";
    bgpath->setText("默认背景");
    setBackground(imageAbsPath);
    SaveConfigFile("BackGroundSet", "imagepath", imageAbsPath);

  });
  bakcground_set_layout->addWidget(bgpath);
  bakcground_set_layout->addWidget(bgbtn);

  // 组件添加外加布局设置
  // 应用到标签
  // 按照一定顺序排列
  layout1->addWidget(lrcset); // 标题
  layout1->addWidget(horizontalLine);
  layout1->addWidget(lyrbtn);
  //------文字
  layout1->addWidget(fontset); // 标题
  layout1->addWidget(horizontalLine1);
  layout1->addLayout(font_choice_layout);
  layout1->addWidget(resetfont);
  //layout1->addLayout(font_size_layout);
  //----背景
  layout1->addWidget(bgset); // 标题
  layout1->addWidget(horizontalLine2);
  layout1->addLayout(bakcground_set_layout);
  layout1->addWidget(bgreset);
  // tab标签的一些设置

  // 垂直布局的一些设置
  layout1->setSpacing(10);
  //layout1->addStretch(); // 布局弹性拉伸
  //加载第二个tab标签
  TabTwo(tabWidget,this);
  // 显示
  tabWidget->show();
  tabWidget->setFixedWidth(300);//窗口宽度

  //tabWidget->setStyleSheet("border-radius: 5px;");

  //按钮设置
  // QDialogButtonBox buttonBox(QDialogButtonBox::Ok,&dialog);
  // connect(&buttonBox, &QDialogButtonBox::accepted, [&dialog]() {
  //     dialog.accept();
  // });
  dialog.setFixedWidth(tabWidget->width());
  dialog.setMinimumHeight(tabWidget->height()+100);
  //buttonBox.move(10,dialog.minimumHeight()-buttonBox.height()-10);
  dialog.setWindowTitle("设置");
  dialog.setWindowFlag(Qt::FramelessWindowHint, false);

  dialog.setWindowModality(Qt::ApplicationModal);

  dialog.exec();
}
void TabTwo(QTabWidget *tabWidget,MainWindow *parent)
{
    //第二个标签
    QWidget *tab = new QWidget();
    tabWidget->addTab(tab, QIcon(), "高级设置");
    QVBoxLayout *layout = new QVBoxLayout(tab);
    //自动播放复选按钮
    QCheckBox *defult_play_music=new QCheckBox("开机自动播放音乐");
    defult_play_music->setChecked(GetCfgBoolValue("Music","isplay"));
    MainWindow::connect(defult_play_music, &QCheckBox::toggled, [](bool checked) {
        if (checked) {
            SaveConfigFile("Music", "isplay", "true");
        } else {
            SaveConfigFile("Music", "isplay", "false");
        }
    });
    //开机是否加载音乐复选按钮
    QCheckBox *load_music_dir=new QCheckBox("开机自动加载音乐列表");
    load_music_dir->setChecked(GetCfgBoolValue("Music","isload"));
    //qDebug()<<GetGeneralValue("Music","isload").toInt();
    MainWindow::connect(load_music_dir, &QCheckBox::toggled, [](bool checked) {
        if (checked) {
            SaveConfigFile("Music", "isload", "true");
        } else {
            SaveConfigFile("Music", "isload", "false");
        }
    });
    //横向布局
    QHBoxLayout *load_music_layout=new QHBoxLayout();
    //默认音乐目录路径标签
    QLabel *default_load_path=new QLabel(GetCfgStringValue("Music","defaultpath"));
    default_load_path->setWordWrap(true);
    //更改默认路径按钮
    QPushButton *change_load_path=new QPushButton();
    change_load_path->setFixedSize(30,30);
    change_load_path->setIcon(QIcon(":/icons/编辑.png"));
    change_load_path->setIconSize(QSize(change_load_path->width() - 15, change_load_path->height() - 15));
    MainWindow::connect(change_load_path, &QPushButton::clicked, [parent, default_load_path]() {
    //默认路径设置
    QString  default_path = QFileDialog::getExistingDirectory(
                parent,                 // 父窗口
                "选择默认路径", // 对话框标题
                QDir::currentPath(),  // 默认打开路径
                QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
        if(!default_path.isNull())
        {
            SaveConfigFile("Music","defaultpath",default_path);
            default_load_path->setText(default_path);
        }
    });
    //分割线------------------------------
    QFrame *horizontalLine = new QFrame(tab);
    horizontalLine->setFrameShape(QFrame::HLine); // 水平线
    horizontalLine->setLineWidth(1);              // 线宽
    //添加组件
    layout->addWidget(defult_play_music);//默认播放音乐
    layout->addWidget(horizontalLine);

    layout->addWidget(load_music_dir);//默认加载音乐
    load_music_layout->addWidget(default_load_path);
    load_music_layout->addWidget(change_load_path);
    layout->addLayout(load_music_layout);

    layout->setSpacing(10);
    layout->addStretch();
}



// 对一些无法进行全局字体设置的组件单独设置字体
void FontSet(QFont font, Ui::MainWindow *ui)
{
    QApplication::setFont(font);
    ui->music_list->setFont(font);
    ui->lyric_display->setFont(font);
    ui->music_list->setFont(font);
    ui->download_list->setFont(font);
    ui->song_data->setFont(font);
}
// 背景选择
QString BackgroungChoice(QWidget *parent)
{
  QString filePath = QFileDialog::getOpenFileName(
      parent, "选择文件", QDir::currentPath(), "图像文件 (*.png *.jpg)");
  return filePath;
}
/*
//单选按钮的设置
QList<QRadioButton *> FontSizeChange(QHBoxLayout *font_size_layout,
                                     Ui::MainWindow *ui) {
  // 设置按钮
  QRadioButton *font_size1 = new QRadioButton("小");
  QRadioButton *font_size2 = new QRadioButton("中");
  QRadioButton *font_size3 = new QRadioButton("大");
  QRadioButton *font_size4 = new QRadioButton("超大");
  font_size_layout->addWidget(font_size1);
  font_size_layout->addWidget(font_size2);
  font_size_layout->addWidget(font_size3);
  font_size_layout->addWidget(font_size4);
  // 字体大小
  int size[4] = {8, 10, 12, 14};
  QList<QRadioButton *> buttons;
  buttons.append(font_size1);
  buttons.append(font_size2);
  buttons.append(font_size3);
  buttons.append(font_size4);
  GetFontSize(buttons);
  for (int i = 0; i < 4; i++) {
    MainWindow::connect(buttons[i], &QRadioButton::toggled,
                        [=](bool checked) {
                          if (checked) {
                            QFont font;
                            font.setPointSize(size[i]);
                            // qDebug()<<checked;
                            FontSet(font, ui);
                            SaveConfigFile("FontSet", "fontsize",
                                           QString::number(size[i]));

                          }
                        });
  }
  return buttons;
}

void GetFontSize(QList<QRadioButton *> buttons) {
  QFont defaultFont = QApplication::font();
  // 获取字体大小（单位为像素）
  int pointSize = defaultFont.pointSize();
  qDebug() << pointSize;
  if (pointSize == fontsize[0]) {
      buttons[0]->setChecked(true);
  } else if (pointSize == fontsize[1]) {
      buttons[1]->setChecked(true);
  } else if (pointSize == fontsize[2]) {
      buttons[2]->setChecked(true);
  } else if (pointSize == fontsize[3]) {
      buttons[3]->setChecked(true);
  }
}
*/
//获取背景文件路径
QString GetBackGround()
{
    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup("BackGroundSet");
    QString imagePath=settings.value("imagepath").toString();
    if(imagePath == ":/image/bk1.jpg")
        imagePath="默认背景";
    return imagePath;
}
//获取字符串类型的值
QString GetCfgStringValue(QString Group , QString param)
{

    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup(Group);
    QString value=settings.value(param).toString();
    return value;
}
//获取布尔类型的值
bool GetCfgBoolValue(QString Group , QString param)
{
    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup(Group);
    bool value=settings.value(param).toBool();
    return value;
}
